<?php
namespace Data;

use Data\Drive\SetArrayKey;
use Data\Drive\ToQuerySql;
use PDO;

class Query extends \Data
{
    public static function Run($sql)
    {
        if (is_array($sql)) {
            isset($sql['key']) && $key = $sql['key'];
            $sql                       = isset($sql['sql']) ? $sql['sql'] : ToQuerySql::Run($sql);
        }
        $data = self::$db->query($sql);
        $code = self::$db->errorCode();
        if ($code != 00000) {
            $info = self::$db->errorInfo();
            self::Error(json_encode($info) . "\r\n" . $sql, __FILE__, __LINE__);
        } else if (!$data || $data->rowCount() == 0) {
            return null;
        }
        $data = $data->fetchAll(PDO::FETCH_ASSOC);
        return isset($key) ? SetArrayKey::Run($data, $key) : $data;
    }
}
